package HRMPROJECT.VIEWS;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.toedter.calendar.JDateChooser;

import HRMPROJECT.BASEUTILS.ConnectionObjects;
import HRMPROJECT.CONTROLLERS.DepartmentController;
import HRMPROJECT.CONTROLLERS.TimeKeepingController;
import HRMPROJECT.MODELS.SecurityContexts;

public class frmMains extends JFrame {

    private final JPanel contentPane;
    private final JTable jtbLoadData;
    private Connection m_objConnection;
    private final JComboBox cboPhongBan;
    private final JDateChooser jdtThoiGian;
    private JLabel lblInfo = null;

    private DepartmentController m_objDepart = null;
    private TimeKeepingController m_objTimeKeep = null;
    private final SecurityContexts m_objSecurity;
    private DefaultTableModel objDtm = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frmMains frame = new frmMains();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frmMains() {
        ConnectionObjects obj = new ConnectionObjects();
        try {
            m_objConnection = obj.OpenConnections();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        m_objSecurity = new SecurityContexts();
        objDtm = new DefaultTableModel();
        m_objDepart = new DepartmentController(m_objConnection, m_objSecurity);
        m_objTimeKeep = new TimeKeepingController(m_objConnection, m_objSecurity);

        // Code tự sinh
        setResizable(false);
        setIconImage(Toolkit.getDefaultToolkit().getImage(frmMains.class.getResource("/HRMPROJECT/IMAGES/Login Manager32x32.png")));
        setTitle("--- CHƯƠNG TRÌNH QUẢN LÝ NHÂN SỰ ---");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 847, 466);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "Th\u00F4ng tin", TitledBorder.LEADING, TitledBorder.TOP, null,
                Color.RED));
        panel.setBounds(0, 0, 640, 428);
        contentPane.add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("Chọn Phòng Ban :");
        lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblNewLabel.setBounds(20, 27, 116, 21);
        panel.add(lblNewLabel);

        JLabel lblChnThiGian = new JLabel("Chọn Thời Gian :");
        lblChnThiGian.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblChnThiGian.setBounds(20, 62, 116, 14);
        panel.add(lblChnThiGian);

        cboPhongBan = new JComboBox();
        cboPhongBan.setFont(new Font("Cambria", Font.PLAIN, 14));
        cboPhongBan.setBounds(135, 24, 265, 27);
        panel.add(cboPhongBan);

        jdtThoiGian = new JDateChooser();
        jdtThoiGian.setDateFormatString("yyyy-MM-dd");
        jdtThoiGian.setBounds(135, 56, 265, 27);
        panel.add(jdtThoiGian);

        JButton btnXemThongTin = new JButton("Xem Thông Tin");
        btnXemThongTin.setBackground(new Color(240, 230, 140));
        btnXemThongTin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                String DayOf = ((JTextField)jdtThoiGian.getDateEditor().getUiComponent()).getText();
                if (DayOf.isEmpty()) {
                    JOptionPane.showMessageDialog(getParent(), "Bạn cần chọn Tháng/Năm báo cáo!");
                    jdtThoiGian.grabFocus();
                } else {
                    // 1. Lấy mã Phòng Ban
                    int DepartId = m_objDepart.getSelectedValue(cboPhongBan, cboPhongBan.getSelectedIndex());

                    // 2. Lấy tháng năm báo cáo
                    //String DayOf = DateConverter.convertDateToString(jdtThoiGian.getDate(), "yyyy-MM-dd");

                    // 3. Thực thi nhận về Models
                    try {
                        objDtm = m_objTimeKeep.getTimeKeepTableModels(DepartId, DayOf);
                        jtbLoadData.setModel(objDtm);
                        lblInfo.setText("Có: " + jtbLoadData.getRowCount() + " bản ghi thỏa mãn ĐK thuộc Phòng: "
                                + cboPhongBan.getSelectedItem());
                    } catch (SQLException e) {
                        System.out.println(e.getMessage());
                    }
                }
            }
        });
        btnXemThongTin.setIcon(new ImageIcon(frmMains.class.getResource("/HRMPROJECT/IMAGES/2.png")));
        btnXemThongTin.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnXemThongTin.setBounds(407, 25, 147, 58);
        panel.add(btnXemThongTin);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 94, 620, 301);
        panel.add(scrollPane);

        jtbLoadData = new JTable();
        scrollPane.setViewportView(jtbLoadData);

        lblInfo = new JLabel("Ready");
        lblInfo.setFont(new Font("Cambria", Font.PLAIN, 14));
        lblInfo.setBounds(10, 399, 620, 18);
        panel.add(lblInfo);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new TitledBorder(null, "Nghi\u1EC7p V\u1EE5", TitledBorder.LEADING, TitledBorder.TOP, null,
                Color.BLUE));
        panel_1.setBounds(650, 11, 171, 281);
        contentPane.add(panel_1);
        panel_1.setLayout(null);

        JButton btnPhongBan = new JButton("1. QL Phòng Ban");
        btnPhongBan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnPhongBan.setBackground(Color.ORANGE);
        btnPhongBan.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnPhongBan.setBounds(10, 26, 151, 33);
        panel_1.add(btnPhongBan);

        JButton btnNhanVien = new JButton("2. QL Nhân Viên");
        btnNhanVien.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frmEmployees frm = new frmEmployees(m_objConnection, m_objSecurity);
                frm.setVisible(true);
            }
        });
        btnNhanVien.setBackground(Color.ORANGE);
        btnNhanVien.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnNhanVien.setBounds(10, 63, 151, 33);
        panel_1.add(btnNhanVien);

        JButton btnChamCong = new JButton("3. QL Chấm Công");
        btnChamCong.setBackground(Color.ORANGE);
        btnChamCong.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnChamCong.setBounds(10, 100, 151, 33);
        panel_1.add(btnChamCong);

        JButton btnTimKiem = new JButton("4. QL Tìm Kiếm");
        btnTimKiem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frmSearchingInfo frm = new frmSearchingInfo(m_objConnection, m_objSecurity);
                frm.setVisible(true);
            }
        });
        btnTimKiem.setBackground(Color.ORANGE);
        btnTimKiem.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnTimKiem.setBounds(10, 138, 151, 33);
        panel_1.add(btnTimKiem);

        JButton btnThoat = new JButton("5. Thoát Ứng Dụng");
        btnThoat.setBackground(Color.RED);
        btnThoat.setFont(new Font("Cambria", Font.PLAIN, 14));
        btnThoat.setBounds(10, 237, 151, 33);
        panel_1.add(btnThoat);

        // Xử lý nghiệp vụ hiển thị thông tin PB lên ComboBox
        try {
            m_objDepart.getDepartmentOf(cboPhongBan);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
